<!-- 获取扫码数据 -->
<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">{{titles}}</span>
				<!-- <i class="iconfont " slot="right-slot" @click="add"></i> -->
			</navitop>
		</div>
		<div :style="'padding-top:' + nv_height"> 
		<!-- 表头 -->
				<div class="bill_sty">
					<div class="bill_list" v-for="(item,index) in dataSorce" v-if="item.dataTab == '表头' && item.code == 'warehouseId' || item.code == 'purchaseTypeId' || item.code == 'vendorId' || item.code == 'crmId'">
						<div class="bill_title">
							{{item.name}}:
						</div>
						<div class="bill_input" @click="choice(item)">
							<input v-model="item.values" disabled/>
						</div>
					</div>
				</div>
				<div class="bill_content" >
					<div class="bill_content_title">
						入库物料
					</div>
					<div class="bill_manual" >
						<div class="bill_manual_choose">
							<span @click="sweep_code">扫码获取</span>
							<span @click="material">选择物料</span>
						</div>
					</div> 
				</div>
				<!-- 明细 -->
				<div class="bill_content" v-for="(item,index) in old_data.detailList" @click="content_click(item)">
					<div class="bill_content_list" >
						<div class="bill_title">物料编码:</div>
						<div class="bill_input_rst">
							{{item.inventoryCode}}
						</div>
					</div>
					<div class="bill_content_list">
						<div class="bill_title">物料名称:</div>
						<div class="bill_input_rst">
							{{item.inventoryName}}
						</div>
					</div>
					<div class="bill_content_list" >
						<div class="bill_title">规格型号:</div>
						<div class="bill_input_rst">
							{{item.inventorySpecification}}
						</div>
					</div>
					
					<div class="bill_content_list">
						<div class="bill_title">实收数量:</div>
						<div class="bill_input" >
							<input v-model="item.baseQuantity" style="border: 0;" disabled/>
						</div>
					</div>
				</div>
			
				<div style="height: 110rpx;"></div>
				<div class="bill_content_button" :style="'width:' + bo_windth" @click="record_up">
					提交审核
				</div>
				
		</div>
		
		<!-- 弹窗组件 -->
		<popus ref="alerts">
			<alertcenter @row="row" @closes="closes" :dataSorc="popup_Data" ></alertcenter>
		</popus>
		
		<popus ref="alert_sub">
			<div :style="'width:' + nv_windowWidth + ';'">
				<div class="alert_out">
					<div class="alert_text" >
						<div class="alert_text_left" >数量：</div>
						<div class="alert_text_right" ><input v-model="quantity_number"/></div>
					</div>
					<div class="alert_button" @click='Number_alert(C_Uid)'>
						<button>确定</button>
					</div>
				</div>
			</div>
		</popus>
		 
		<!-- 扫码 -->
		<moncode :Scan_code="scancode"></moncode>
		
		<!-- 下拉修改弹窗 -->
		<popus ref="edit_list">
			<div class="conten">
				<div class="conten_edit">
					<div class="conten_edit_type conten_edit_public">
						<div class="conten_edit_left">
							物料编码：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventoryCode}}
						</div>
					</div>
					<div class="conten_edit_type">
						<div class="conten_edit_left">
							物料名称：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventoryName}}
						</div>
					</div>
					<div class="conten_edit_type">
						<div class="conten_edit_left">
							规格型号：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventorySpecification}}
						</div>
					</div>
					<div class="conten_edit_type ">
						<div class="conten_edit_left">
							数量：
						</div>
						<div class="conten_edit_right conten_edit_inputs">
							<div  class="conten_edit_input">
								<input v-model="click_data.baseQuantity" type="number"/>
							</div>
							<div class="conten_edit_plus">
								<div class="conten_edit_bod conten_edit_right_bod" @click="number_plus">
									+
								</div>
								<div class="conten_edit_bod" @click="number_dom">
									-
								</div>
							</div>
						</div>
					</div>
				
				</div>
				<div class="conten_del" @click="del_list">
					<i class="iconfont icon-shanchu"></i>
				</div>
				<div class="conten_edit_bottom" @click="content_close">
					保&nbsp;&nbsp;存
				</div>
			</div>
		</popus>
		
		
		
		<!-- 搜索上拉 -->
		<popus ref="science_al">
			<div :style="'width:' + bo_windth " class="bill_wiat">
				<sciencealer @row="row" @closes="closes" :dataSorc="popup_Data"></sciencealer>
			</div>
		</popus>
	</view>
</template>

<script>
import navitop from '@/components/common/navi-top'
import alertcenter from '@/pages/common/common/alert/alertcenter'
import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
import moncode from "@/components/dumenghanfeng-monitorScanCode/monitorScanCode.vue"
import {current_time} from "@/common/api/common/public_api.js"
import sciencealer from '@/pages/common/common/alert/sciencealert'
import {inpageList,list,listhouses,pageList,pageForVoucher,getList,inventorypageList,recordsave,ForVoucher,BarCode,nssave,savecode,DynamicById} from '@/common/api/job/bill.js'
export default {
	components: {
		navitop,
		alertcenter,
		popus,
		moncode,
		sciencealer
	},
	data(){
		return {
			titles: '',
			nv_height: '',
			nv_windowWidth: '',
			bo_windth: '',
			z_height: '',
			dataSorce: '', // 获取表字段
			popup_Data: [],	// 获取弹窗数据
			data_curl_rst: {current: 1, size: 10, keyWord: '', 'page.current':1, 'page.size': 10},
			
			Code_dataSorce: [],	// 获取用户扫码结果
			
			scancode: 'Warehouse',	// 扫码自定义事件
			
			curl_data: '',
			
			quantity_number: '',
			
			baseQuantity: '',
			
			C_Uid: '',	// 区分数量(那个的)
			
			D_code: '',	// 分类
			
			title_data: [],
			
			deliver_tpye: 0,
			
			rundata: 0,
			
			mainId: null,	// 发货单主表ID
			detaiMap: new Map(),
			
			old_data: [],
			
			click_data:'',
			
			the_old_dataSorce: [],
			
			thie_old_uid:'',
			
		}
	},
	onLoad(option) {
		this.D_code = option.code;
		this.curl_data = option;
		this.titles = option.title;
		if(option.code == "NSSaleDispatchVoucher"){
			this.deliver_tpye = 1;
		}
	
		let _this = this;
		uni.$off(this.scancode);
		uni.$on(this.scancode, function(rst){
			console.log('扫码结果',rst.msg)
			// 扫码去重
			if(option.code == "NSSaleDispatchVoucher"){
				if(_this.title_data.length == 0){
					_this.title_data.push(rst.msg);
					_this.deliver_list(rst.msg);
				}
				if(_this.title_data.indexOf(rst.msg) == -1){
					_this.deliver_list(rst.msg);
					_this.title_data.push(rst.msg);
				}
			}else{
				if(_this.title_data.length == 0){
					_this.title_data.push(rst.msg);
					_this.inventory_list(rst.msg);
				}
				if(_this.title_data.indexOf(rst.msg) == -1){
					_this.inventory_list(rst.msg);
					_this.title_data.push(rst.msg);
				}
			}
		
		})
		
	},
	mounted() {
		this.nv_height = uni.getSystemInfoSync().statusBarHeight + 60 + 'px';
		
		this.nv_windowWidth = uni.getSystemInfoSync().windowWidth - 20 + 'px';
		
		this.bo_windth =  uni.getSystemInfoSync().windowWidth + 'px';
		
		this.z_height = uni.getSystemInfoSync().windowHeight + 'px';
		
		this.bill_list();
		
	},
	methods: {
		
		// 返回
		outlot(){
			uni.navigateBack()
		},
	

		// 点击添加数量
		number_plus(){
			if(Number(this.click_data.baseQuantity)> 0){
				this.click_data.baseQuantity = Number(this.click_data.baseQuantity) + 1;
			}else{
				this.click_data.baseQuantity = Number(this.click_data.baseQuantity) - 1;
			}
		},
		
		// 点击减少数量
		number_dom(){
			if(Number(this.click_data.baseQuantity) > 0){
				this.click_data.baseQuantity = Number(this.click_data.baseQuantity) - 1;
			}else {
				this.click_data.baseQuantity = Number(this.click_data.baseQuantity) + 1;
			}
		},
		
		// 弹窗保存
		content_close(){
			this.$refs.edit_list.close();
		},
		
		//  点击获取数据
		content_click(val){
			this.click_data = val;
			this.$refs.edit_list.open('bottom')
		},
		
		// 点击删除
		del_list(){
			for(let i=0 ; i< this.old_data.detailList.length; i++){
				if(this.click_data.id == this.old_data.detailList[i].id){
					this.old_data.detailList.splice(i,1);
				}
			}
			this.$refs.edit_list.close();
		},
		
		// 回显数据
		data_list(){
			let _this = this;
			let obj_data = [];
			uni.showLoading({
				title: '加载中...'
			})
			obj_data = uni.getStorageSync('details_dataSorce')
			DynamicById({currentId: this.curl_data.id,source: this.curl_data.code,isMyWarehouse: true,someBusinessType: this.curl_data.sort}).then((rows) => {
				if(rows.code == 200){
					this.thie_old_uid = rows.data.id;
					uni.hideLoading();
					this.dataSorce.filter((item) => {
						this.$set(item,'values',rows.data[item.code.replace('Id',"Name")])
						this.$set(item,'value',rows.data[item.code])
					})
					this.old_data = rows.data;
					if(obj_data){
						obj_data.filter(item => {
							item.warehouseName = '';
							item.warehouseId = '';
							_this.old_data.detailList.push(item)
						})
					}
				}
			})
		},
		
		// 点击获取物料信息 
		material(){
			uni.navigateTo({
				url: '/components/common/bill/bill-details?title=' + this.curl_data.title + 
				'&sort=' + this.curl_data.sort + 
				'&key=' + this.curl_data.key + 
				'&c_code=' + this.curl_data.c_code + 
				'&code=' + this.curl_data.code +
				'&id=' + this.curl_data.id
			})
		},
		
		// 点击获取扫码(相机)
		sweep_code(){
			let _this = this;
			uni.scanCode({
			    onlyFromCamera: true,
			    success: function (res) {
			        console.log('条码类型：' + res.scanType);
			        console.log('条码内容：' + res.result);
					_this.inventory_list(res.result);
			    }
			});
		},
		
		// 扫码获取物料
		inventory_list(val){ 
			let _this = this;
			inventorypageList({current: 1,size: 20,inventoryCode: val}).then((rows) => {
				if(rows.code == 200){
					rows.data.records.filter((item) => {
						_this.C_Uid = item.id;
						_this.old_data.detailList.push(item);
					})
					this.$refs.alert_sub.open('center'); 
				}
			})
		},
		
		
		//	点击获取输入数量
		Number_alert(){
			this.baseQuantity = this.quantity_number;
			let _this = this;
			this.Code_dataSorce.forEach((item) => {
				if(item.id == _this.C_Uid){
					this.$set(item,'baseQuantity',_this.quantity_number);
				}
			})
			this.$refs.alert_sub.close();
		},
		
		// 修改数量
		Nub_alert(){
			this.$refs.alert_sub.open('center');
		},
		
		
		// 获取列表字段
		bill_list(){
			let _this = this;
			let pot = {};
			list({source: this.D_code}).then((rows) => {
				if(rows.code == 200){
					this.dataSorce = rows.data;
					if(this.dataSorce){
						this.data_list();
					}
				}
			})
		},
		
		// 弹窗处理(数据处理)
		choice(val){
			this.popup_Data = [];
			if(val.code == "warehouseId"){
				this.warehouse_list(val.code);
			}
			if(val.code == 'purchaseTypeId') {
				this.purchase_list(val.code);
			}
			if(val.code == 'vendorId'){
				this.voucher_list(val.code);
			}
			if(val.code == 'businessType'){
				let run = JSON.parse(val.val);
				run.forEach((item) => {
					this.$set(item,'name',item.label);
					this.$set(item,'id',item.key);
					this.$set(item,'code','businessType')
				})
				this.popup_Data = run;
			}
			if(val.code == 'crmId'){
				this.User_list('crmId');
			}
			
			this.$refs.science_al.open('bottom'); 
		},
		
		// 弹窗处理(获取弹窗结果)
		row(val){
			let pot = {};
			if(val.code !== 'businessType'){
				pot['value'] = val.name;
				pot['code'] = val.code.split('Id')[0] + 'Name';
				this.dataSorce.filter((item) => {
					if(item.code == val.code){
						if(val.code.substr(val.code.length-2,2) == 'Id'){
							this.$set(item,'values',val.name)
							this.$set(item,'value',val.id)
						}else {
							this.$set(item,'value',val.name)
						}
					}
				})
				this.dataSorce.push(pot);
			}else{
				this.dataSorce.filter((item) => {
					if(item.code == val.code){
						this.$set(item,'values',val.name);
						this.$set(item,'value',val.id);
					}
				})
			}
			this.$refs.science_al.close();
		},
		
		// 获取客户
		User_list(valCode){
			let _this = this;
			ForVoucher({current: 1, size: 10, isFinance: 1, companyType: 'isCustomer', 'page.current': 1,'page.size': 10}).then((rst) => {
			if(rst.code == 200){
				rst.data.records.filter((item) => {
					_this.popup_Data.push({name:item.crmName,code:valCode,id:item.id})
				})
			}
			})
		},
		
		// 获取仓库
		warehouse_list(valCode){
			let _this = this;
			listhouses(this.data_curl_rst).then((rst) => {
				if(rst.code == 200){
					rst.data.records.filter((item) => {
						_this.popup_Data.push({name:item.whName,code:valCode,id:item.id,whCode: item.whCode})
					})
				}
			})
		},
		
		// 获取采购列表
		purchase_list(valCode){
			let _this = this;
			pageList(this.data_curl_rst).then((rows) => {
				if(rows.code == 200){
					rows.data.records.filter((item) => {
						_this.popup_Data.push({name:item.name,code:valCode,id:item.id,D_Code: item.code})
					})
				}
			})
		},
		
		// 获取供应商
		
		voucher_list(valCode){
			let _this = this;
			let coucherdata = {current: 1,size: 10,isFinance: 1,companyType: 'isSupplier','page.current': 1,'page.size': 10};
			pageForVoucher(coucherdata).then((rows) => {
				if(rows.code == 200){
					rows.data.records.filter((item) => {
						_this.popup_Data.push({name:item.crmName,code:valCode,id:item.id,D_Code: item.crmNo})
					})
				}
			})
		},
		
		// 提交 
		record_up(){
			let dats = {};
			this.dataSorce.filter((item) => {
				if(item.code.substr(item.code.length-2,2) == 'Id'){
					dats[item.code] = item.value
				}else{
					dats[item.code] = item.values
				}
			})
			delete dats.createUserId;
			dats['orderDetailList'] = this.old_data.detailList;
			dats['dataSource'] = this.D_code;
			dats['reviewerDate'] = current_time();
			if(this.curl_data.c_code == 1){
				// dats['inDate'] = current_time();
				// dats['receiveDeliverType'] = this.curl_data.c_code;
			}
			if(this.curl_data.c_code == 2){
				// dats['deliveryDate'] = current_time();
				// dats['receiveDeliverType'] = this.curl_data.c_code;
			}
			if(this.curl_data.c_code == 3){
				dats['businessType'] == 99;
				// dats['receiveDeliverType'] = 1;
			}
			
			delete dats.createDate
			delete dats.inDate
			delete dats.reviewerDate
			delete dats.voucherDate
			dats.id = this.thie_old_uid;
			recordsave(dats).then((rows) => {
				if(rows.code == 200){
					uni.showToast({
						title: '提交成功！'
					})
					setTimeout(()=>{
						uni.removeStorageSync('details_dataSorce');
						uni.navigateTo({
							url: '/pages/common/index/purchase/common?title=' + this.curl_data.title + '&code=' + this.curl_data.code + '&sort=' + this.curl_data.sort + '&c_code=' + 1
						})
					},1500)
				}else {
					uni.removeStorageSync('details_dataSorce');
					uni.showToast({
						title: rows.message,
						icon: "none"
					})
				}
			})
		},
		

	
	}
}	
</script>

<style scoped lang="scss">
	
	
	// 上拉修改弹窗
	
	.conten_del {
		position: fixed;
		top: 0;
		right: 5%;
		color: red;
		padding: 10px 10px 10px 10px;
		font-size: 20px;
	}
	
	.conten_edit_input {
		width: 50%;
	}
	
	.conten_edit_inputs {
		display: flex;
		justify-content: space-between;
		input {
			border: 0;
		}
	}
	
	.conten_edit_bottom {
		height: 50px;
		line-height: 50px;
		text-align: center;
		background-color: #1890FF;
		color: #fff;
		font-size: 18px;
		border-radius: 4px;
		box-shadow: 0 12px 2px 0 rgba(0,0,0,0.1);
	}
	
	.conten_edit_public {
		padding-top: 5px;
	}
	
	.conten_edit_bod {
		border-top: 0 !important;
		border-bottom: 0 !important;
	}
	
	.conten_edit_right_bod {
		border-right: 0 !important;
	}
	
	.conten_edit_plus{
		display: flex;
		text-align: center;
		div {
			border: 1px dotted #cacaca;
			width: 80rpx;
		}
	}
	
	.conten_edit_type {
		display: flex;
		border-bottom: 1px dotted #cacaca;
		// overflow: hidden;
	}
	.conten_edit_left {
		width: 30%;
	}
	
	.conten_edit_right {
		width: 70%;
	}
	
	.conten_edit {
		height: 240px;	
		width: 90%;
		margin: auto;
		div {
			height: 80rpx;
			line-height: 80rpx;
		}
	}
	.conten {
		width: 100%;
		background-color: #fff;
	}
	
	.remaks_data {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		color: #cacaca;
	}
	
	// 产品选择样式
	.bill_manual {
		background-color: #fff;
		text-align: center;
		width: 100%;
		margin: auto;
	}
	
	.bill_manual_choose {
		border-top: 1px solid #cacaca;
		padding: 10px 0 0 0;
		width: 95%;
		margin: auto;
		height: 90rpx;
		line-height: 90rpx;
		span {
			padding: 1% 8% 1% 8%;
			border-radius: 4px;
			background-color: #007AFF;
			color: #fff;
			margin: 10px;
		}
	}
	
	// 备注模块
	
	.remarks_inpt {
		width: 95%;
		margin: auto;
		padding: 0 0 90rpx 0;
		textarea {
			width: 100%;
			height: 120rpx;
			margin: 10px 0 10px 0;
		}
	}
	
	.remarks_txt {
		width: 95%;
		margin: auto;
		height: 60rpx;
		line-height: 60rpx;
		border-bottom: 1px solid #cacaca;
	}
	
	.remarks_style {
		margin: 10px 0 0 0;
		background-color: #fff;
		box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
	}
	

	.bill_content_button {
		height: 90rpx;
		line-height: 90rpx;
		background-color:  rgb(34, 131, 228);
		text-align: center;
		color: #fff;
		border-radius: 4px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
		margin: 10px 0 0 0;
		position: fixed;
		bottom: 0;
	}
	.alert_button {
		padding: 0 0 5px 0;
		button {
			background-color: rgb(34, 131, 228);
			color: #fff;
		}
	}
	.alert_out {
		background-color: #fff;
		border-radius: 4px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	}
	.alert_text{
		display: flex;
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		margin: 10px 0 10px 0;
		padding: 20px 0 20px 0;
	}
	
	.alert_text_left {
		width: 30%;
		text-align: right;
	}
	
	.bill_sty {
		background-color: #fff;
		padding: 5px 0 15px 0;
	}
	.bill_list {
		width: 90%;
		margin: auto;
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 0 0 30rpx;
		display: flex;
	}
	
	.bill_title {
		width: 30%;
		overflow: hidden;
		text-align: left;
	}
	
	.alert_text_right {
		width: 68%;
		input {
			border: 1px solid #cacaca;
			width: 90%;
		}
	}
	
	.bill_input {
		width: 65%;
		input {
			width: 90%;
			border: 1px solid #cacaca;
			margin-left: 10px;
			height: 50rpx;
		}
	}
	
	.bill_input_rst {
		width: 65%;
		margin: 0 0 0 20rpx;
	}
	
	// 数据列表
	.bill_content {
		background-color: #fff;
		margin-top: 10px;
		padding: 10rpx 0 10px 0;
	}
	
	.bill_content_mx {
		background-color: #fff;
	}
	
	.bill_content_list {
		width: 90%;
		margin: auto;
		display: flex;
		height: 60rpx;
		line-height: 60rpx;
	}
	
	.bill_content_title {
		padding: 10rpx 0 10rpx 30rpx;
		margin-bottom: 5px ;
	}
	
</style>
